package service;

import java.util.ArrayList;
import java.util.List;

import modelo.*;
import beans.*;
import entities.*;

public class ComandaSrv {
	
	private static ComandaSrv instancia = null;
	
	public static ComandaSrv getInstancia() {
		if(instancia == null) {
			instancia = new ComandaSrv();
		}
		return instancia;
	}

	/**
	 * 
	 * @param comandaBean
	 */
	public void guardarComanda(ComandaBean comandaBean) {
	ComandaEntity comandaEntity = this.bean2Entity(comandaBean);
	ComandaDao.getInstancia().guardar(comandaEntity);	
	}
	
	/**
	 * 
	 * @param comandaBean
	 */
	public void modificarComanda(ComandaBean comandaBean) {
	ComandaEntity comandaEntity = this.bean2Entity(comandaBean);
	ComandaDao.getInstancia().merge(comandaEntity);	
	}

	/**
	 * 
	 * @param comandaBean
	 * @return
	 */
	public ComandaEntity bean2Entity(ComandaBean comandaBean) {
		ComandaEntity comandaEntity = new ComandaEntity();
		comandaEntity.setId(comandaBean.getId());
		comandaEntity.setEstado(comandaBean.getEstado());
		//comandaEntity.setAperturaMesa(AperturaMesaSrv.getInstancia().bean2Entity(comandaBean.getAperturaMesa()));
		comandaEntity.setItem(ItemCartaSrv.getInstancia().bean2Entity(comandaBean.getItem()));
		return comandaEntity;
	}
	
	/**
	 * 
	 * @param comandaEntity
	 * @return
	 */
	public ComandaBean entity2Bean(ComandaEntity comandaEntity) {
		ComandaBean comandaBean = new ComandaBean();
		comandaBean.setId(comandaEntity.getId());
		comandaBean.setEstado(comandaEntity.getEstado());
		//comandaBean.setAperturaMesa(AperturaMesaSrv.getInstancia().entity2Bean(comandaEntity.getAperturaMesa()));
		//comandaBean.setPedido(PedidoSrv.getInstancia().entity2Bean(comandaEntity.getPedido()));
		comandaBean.setItem(ItemCartaSrv.getInstancia().entity2Bean(comandaEntity.getItem()));
		return comandaBean;
	}
	
	/**
	 * 
	 * @param area
	 * @return
	 */
	public List<ComandaBean> obtenerComandasPorArea(String area)
	{
		List<ComandaEntity> comandasEntity = ComandaDao.getInstancia().obtenerComandasPorArea(area);
		List<ComandaBean> comandasBean = new ArrayList<ComandaBean>();
		for (ComandaEntity comandaEntity: comandasEntity) {
			ComandaBean comandaBean = entity2Bean(comandaEntity);
			comandasBean.add(comandaBean);
		}
		return comandasBean;
	}
	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public ComandaBean obtenerComandasPorId(Integer id) 
	{
		ComandaBean comanda = null;	
		comanda= entity2Bean(ComandaDao.getInstancia().obtenerComandasPorId(id));
		return comanda;
	}	
	
}
